#!/usr/bin/python
import time
from lightPlayer import lightPlayer
from socket_server import SocketServerClass
from socket_server_mock_1 import ServerClass
from multiprocessing import Process
from mqtt_client import WuxiMqttClass
import signal
import os


def term(sig_num, addtion):
    print('term current pid is %s, group id is %s' % (os.getpid(), os.getpgrp()))
    os.killpg(os.getpgid(os.getpid()), signal.SIGKILL)


signal.signal(signal.SIGTERM, term)


def mq_run(broker, port, obj):
    # lightPlayer.setLightColor({"R":60, "G":60, "B":60})
    mq = WuxiMqttClass(broker, port, obj)
    mq.subscribe_topic_received("wuxi/#")
    mq.run()


def transmitAudioStream(lightPlayer):
    ss = ServerClass()
    ss.start_server(lightPlayer)




if __name__ == '__main__':
    mq_broker = '192.168.211.179'
    mq_port = 1883

    socket_host = '192.168.211.251'
    socket_port = 8080

    lightPlayer.start()
    print('------------------------------------')
    # lightPlayer.setLightColor({"R":0, "G":0, "B":60})
    tcp_process = Process(target=transmitAudioStream, args=(lightPlayer))
    tcp_process.start()
    mq_process = Process(target=mq_run, args=(mq_broker, mq_port, lightPlayer))
    mq_process.start()
    tcp_process.join()
    # mq_process.join()
